package com.ssyx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssyx.activity.ActivityFeignClient;
import com.ssyx.auth.AuthThreadLocal;
import com.ssyx.cart.CartFeignClient;
import com.ssyx.constant.GlobalConstant;
import com.ssyx.constant.RabbitMQConstant;
import com.ssyx.constant.RedisConstant;
import com.ssyx.enums.*;
import com.ssyx.exception.SsyxException;
import com.ssyx.mapper.OrderInfoMapper;
import com.ssyx.mapper.OrderItemMapper;
import com.ssyx.model.activity.ActivityRule;
import com.ssyx.model.activity.CouponInfo;
import com.ssyx.model.order.CartInfo;
import com.ssyx.model.order.OrderInfo;
import com.ssyx.model.order.OrderItem;
import com.ssyx.product.ProductFeignClient;
import com.ssyx.result.ResultCodeEnum;
import com.ssyx.service.OrderInfoService;
import com.ssyx.service.RabbitMQService;
import com.ssyx.user.UserFeignClient;
import com.ssyx.utils.DateUtil;
import com.ssyx.vo.order.CartInfoVo;
import com.ssyx.vo.order.OrderConfirmVo;
import com.ssyx.vo.order.OrderSubmitVo;
import com.ssyx.vo.order.OrderUserQueryVo;
import com.ssyx.vo.product.SkuStockLockVo;
import com.ssyx.vo.user.LeaderAddressVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
	
	@Resource
	private UserFeignClient userFeignClient;
	@Resource
	private CartFeignClient cartFeignClient;
	@Resource
	private ActivityFeignClient activityFeignClient;
	@Resource
	private ProductFeignClient productFeignClient;
	@Resource
	private RedisTemplate<String, Object> redisTemplate;
	@Resource
	private RabbitMQService rabbitMQService;
	@Resource
	private OrderItemMapper orderItemMapper;
	
	//确认订单
	@Override
	public OrderConfirmVo confirmOrder() {
		// 获取用户id
		Long userId = AuthThreadLocal.getId();
		// 获取用户对应团长信息
		LeaderAddressVo leaderAddressVo =
				userFeignClient.getLeaderAddressVoByUserId(userId);
		//获取购物车里面选中的商品
		List<CartInfo> cartInfoList = cartFeignClient.getCartCheckedList(userId);
		
		//唯一标识订单
		String orderNo = System.currentTimeMillis() + "";
		redisTemplate.opsForValue().set(RedisConstant.ORDER_REPEAT + orderNo, orderNo,
				RedisConstant.SKULOCK_EXPIRE_PX1, TimeUnit.DAYS);
		
		//获取购物车满足条件活动和优惠卷信息
		OrderConfirmVo orderConfirmVo =
				activityFeignClient.findCartActivityAndCoupon(cartInfoList, userId);
		//封装其他值
		orderConfirmVo.setLeaderAddressVo(leaderAddressVo);
		orderConfirmVo.setOrderNo(orderNo);
		
		return orderConfirmVo;
	}
	
	//生成订单
	@Override
	public Long submitOrder(OrderSubmitVo orderParamVo) {
		//第一步 设置给哪个用户生成订单  设置orderParamVo的userId
		Long userId = AuthThreadLocal.getId();
		orderParamVo.setUserId(userId);
		//第二步 订单不能重复提交，重复提交验证
		// 通过redis + lua脚本进行判断 lua脚本保证原子性操作
		// 获取传递过来的订单 orderNo
		String orderNo = orderParamVo.getOrderNo();
		if (StringUtils.isEmpty(orderNo)) {
			log.error("传递过来的订单号为空");
			throw new SsyxException(ResultCodeEnum.ILLEGAL_REQUEST);
		}
		// 拿着orderNo 到 redis进行查询，
		String script = "if(redis.call('get', KEYS[1]) == ARGV[1]) then return redis.call('del', KEYS[1]) else return " +
				"0 " +
				"end";
		//3 如果redis有相同orderNo，表示正常提交订单，把redis的orderNo删除
		Boolean flag = (Boolean) redisTemplate
				.execute(new DefaultRedisScript(script, Boolean.class),
						Collections.singletonList(RedisConstant.ORDER_REPEAT + orderNo), orderNo);
		//4 如果redis没有相同orderNo，表示重复提交了，不能再往后进行
		if (Boolean.FALSE.equals(flag)) {
			log.error("订单号重复提交");
			throw new SsyxException(ResultCodeEnum.REPEAT_SUBMIT);
		}
		// 验证库存 并且 锁定库存
		// 比如仓库有10个西红柿，我想买2个西红柿
		// ** 验证库存，查询仓库里面是是否有充足西红柿
		// ** 库存充足，库存锁定 2锁定（目前没有真正减库存）
		// 远程调用service-cart模块，获取当前用户购物车商品（选中的购物项）
		List<CartInfo> cartInfoList =
				cartFeignClient.getCartCheckedList(userId);
		//购物车有很多商品，商品不同类型，重点处理普通类型商品
		List<CartInfo> commonSkuList =
				cartInfoList.stream().filter(cartInfo -> cartInfo.getSkuType().equals(SkuType.COMMON.getCode())).collect(Collectors.toList());
		// 把获取购物车里面普通类型商品list集合，
		// List<CartInfo>转换List<SkuStockLockVo>
		if (!CollectionUtils.isEmpty(commonSkuList)) {
			List<SkuStockLockVo> commonStockLockVoList = commonSkuList.stream().map(item -> {
				SkuStockLockVo skuStockLockVo = new SkuStockLockVo();
				skuStockLockVo.setSkuId(item.getSkuId());
				skuStockLockVo.setSkuNum(item.getSkuNum());
				return skuStockLockVo;
			}).collect(Collectors.toList());
			// 远程调用service-product模块实现锁定商品 验证库存并锁定库存，保证具备原子性
			Boolean isLockSuccess =
					productFeignClient.checkAndLock(commonStockLockVoList, orderNo);
			if (!isLockSuccess) {
				//库存锁定失败
				log.error("库存锁定失败");
				throw new SsyxException(ResultCodeEnum.ORDER_STOCK_FALL);
			}
		}
		//第四步 下单过程
		// 向两张表添加数据
		Long orderId = saveOrder(orderParamVo, cartInfoList);
		
		//下单完成，删除购物车记录
		//发送mq消息
		rabbitMQService.sendMessage(RabbitMQConstant.EXCHANGE_ORDER_DIRECT,
				RabbitMQConstant.ROUTING_DELETE_CART, orderParamVo.getUserId());
		// 返回订单id
		return orderId;
	}
	
	//订单详情
	@Override
	public OrderInfo getOrderInfoById(Long orderId) {
		//根据orderId查询订单基本信息
		OrderInfo orderInfo = getById(orderId);
		//根据orderId查询订单所有订单项list列表
		List<OrderItem> orderItemList =
				orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderId,
						orderInfo.getId()));
		//查询所有订单项封装到每个订单对象里面
		orderInfo.setOrderItemList(orderItemList);
		return orderInfo;
	}
	
	//根据orderNo查询订单信息
	@Override
	public OrderInfo getOrderInfoByOrderNo(String orderNo) {
		return getOne(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getOrderNo, orderNo));
	}
	
	//订单支付成功，更新订单状态，扣减库存
	@Override
	public void orderPay(String orderNo) {
		//查询订单状态是否已经修改完成了支付状态
		OrderInfo orderInfo = getOrderInfoByOrderNo(orderNo);
		if (orderInfo == null || !orderInfo.getOrderStatus().equals(OrderStatus.UNPAID.getCode())) {
			return;
		}
		//更新状态
		this.updateOrderStatus(orderInfo.getId());
		//扣减库存
		rabbitMQService.sendMessage(RabbitMQConstant.EXCHANGE_ORDER_DIRECT, RabbitMQConstant.ROUTING_MINUS_STOCK,
				orderNo);
	}
	
	//订单查询
	@Override
	public IPage<OrderInfo> findUserOrderPage(Page<OrderInfo> pageParam,
	                                          OrderUserQueryVo orderUserQueryVo) {
		IPage<OrderInfo> pageModel = baseMapper.selectPage(pageParam,
				Wrappers.<OrderInfo>lambdaQuery().eq(OrderInfo::getUserId, orderUserQueryVo.getUserId()).eq(OrderInfo::getOrderStatus, orderUserQueryVo.getOrderStatus()));
		
		//获取每个订单，把每个订单里面订单项查询封装
		List<OrderInfo> orderInfoList = pageModel.getRecords();
		for (OrderInfo orderInfo : orderInfoList) {
			//根据订单id查询里面所有订单项列表
			List<OrderItem> orderItemList =
					orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderId,
							orderInfo.getId()));
			//把订单项集合封装到每个订单里面
			orderInfo.setOrderItemList(orderItemList);
			//封装订单状态名称
			orderInfo.getParam().put("orderStatusName", orderInfo.getOrderStatus());
		}
		return pageModel;
	}
	
	// 向两张表添加数据
	@Transactional(rollbackFor = {Exception.class})
	public Long saveOrder(OrderSubmitVo orderParamVo,
	                      List<CartInfo> cartInfoList) {
		if (CollectionUtils.isEmpty(cartInfoList)) {
			throw new SsyxException(ResultCodeEnum.DATA_ERROR);
		}
		//查询用户提货点和团长信息
		Long userId = AuthThreadLocal.getId();
		LeaderAddressVo leaderAddressVo = userFeignClient.getLeaderAddressVoByUserId(userId);
		if (leaderAddressVo == null) {
			throw new SsyxException(ResultCodeEnum.DATA_ERROR);
		}
		//计算金额
		//营销活动金额
		Map<String, BigDecimal> activitySplitAmount = computeActivitySplitAmount(cartInfoList);
		//优惠卷金额
		Map<String, BigDecimal> couponInfoSplitAmount = computeCouponInfoSplitAmount(cartInfoList,
				orderParamVo.getCouponId());
		//封装订单项数据
		List<OrderItem> orderItemList = new ArrayList<>();
		for (CartInfo cartInfo : cartInfoList) {
			OrderItem orderItem = new OrderItem();
			orderItem.setId(null);
			orderItem.setCategoryId(cartInfo.getCategoryId());
			if (cartInfo.getSkuType().equals(SkuType.COMMON.getCode())) {
				orderItem.setSkuType(SkuType.COMMON);
			} else {
				orderItem.setSkuType(SkuType.SECKILL);
			}
			orderItem.setSkuId(cartInfo.getSkuId());
			orderItem.setSkuName(cartInfo.getSkuName());
			orderItem.setSkuPrice(cartInfo.getCartPrice());
			orderItem.setImgUrl(cartInfo.getImgUrl());
			orderItem.setSkuNum(cartInfo.getSkuNum());
			orderItem.setLeaderId(orderParamVo.getLeaderId());
			//营销活动金额
			BigDecimal activityAmount =
					activitySplitAmount.get("activity:" + orderItem.getSkuId());
			if (activityAmount == null) {
				activityAmount = new BigDecimal(GlobalConstant.INTEGER_ZERO);
			}
			orderItem.setSplitActivityAmount(activityAmount);
			
			//优惠卷金额
			BigDecimal couponAmount = couponInfoSplitAmount.get("coupon:" + orderItem.getSkuId());
			if (couponAmount == null) {
				couponAmount = new BigDecimal(GlobalConstant.INTEGER_ZERO);
			}
			orderItem.setSplitCouponAmount(couponAmount);
			
			//总金额
			BigDecimal skuTotalAmount =
					orderItem.getSkuPrice().multiply(new BigDecimal(orderItem.getSkuNum()));
			//优惠之后金额
			BigDecimal splitTotalAmount =
					skuTotalAmount.subtract(activityAmount).subtract(couponAmount);
			orderItem.setSplitTotalAmount(splitTotalAmount);
			orderItemList.add(orderItem);
		}
		
		//封装订单OrderInfo数据
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setUserId(userId);//用户id
		orderInfo.setOrderNo(orderParamVo.getOrderNo()); //订单号 唯一标识
		orderInfo.setOrderStatus(OrderStatus.UNPAID.getCode()); //订单状态，生成成功未支付
		orderInfo.setLeaderId(orderParamVo.getLeaderId());//团长id
		orderInfo.setLeaderName(leaderAddressVo.getLeaderName());//团长名称
		
		orderInfo.setLeaderPhone(leaderAddressVo.getLeaderPhone());
		orderInfo.setTakeName(leaderAddressVo.getTakeName());
		orderInfo.setReceiverName(orderParamVo.getReceiverName());
		orderInfo.setReceiverPhone(orderParamVo.getReceiverPhone());
		orderInfo.setReceiverProvince(leaderAddressVo.getProvince());
		orderInfo.setReceiverCity(leaderAddressVo.getCity());
		orderInfo.setReceiverDistrict(leaderAddressVo.getDistrict());
		orderInfo.setReceiverAddress(leaderAddressVo.getDetailAddress());
		orderInfo.setWareId(cartInfoList.get(GlobalConstant.INTEGER_ZERO).getWareId());
		orderInfo.setProcessStatus(ProcessStatus.UNPAID.getCode());
		
		//计算订单金额
		BigDecimal originalTotalAmount = this.computeTotalAmount(cartInfoList);
		BigDecimal activityAmount = activitySplitAmount.get("activity:total");
		
		if (activityAmount == null) activityAmount = new BigDecimal(GlobalConstant.INTEGER_ZERO);
		BigDecimal couponAmount = couponInfoSplitAmount.get("coupon:total");
		
		if (couponAmount == null) couponAmount = new BigDecimal(GlobalConstant.INTEGER_ZERO);
		BigDecimal totalAmount = originalTotalAmount.subtract(activityAmount).subtract(couponAmount);
		//计算订单金额
		orderInfo.setOriginalTotalAmount(originalTotalAmount);
		orderInfo.setActivityAmount(activityAmount);
		orderInfo.setCouponAmount(couponAmount);
		orderInfo.setTotalAmount(totalAmount);
		
		//计算团长佣金
		BigDecimal profitRate = new BigDecimal(GlobalConstant.INTEGER_ZERO);//orderSetService.getProfitRate();
		BigDecimal commissionAmount = orderInfo.getTotalAmount().multiply(profitRate);
		orderInfo.setCommissionAmount(commissionAmount);
		//添加数据到订单基本信息表
		save(orderInfo);
		//添加订单里面订单项
		orderItemList.forEach(orderItem -> {
			orderItem.setOrderId(orderInfo.getId());
			orderItemMapper.insert(orderItem);
		});
		
		//如果当前订单使用优惠卷，更新优惠卷状态
		if (orderInfo.getCouponId() != null) {
			activityFeignClient.updateCouponInfoUseStatus(orderInfo.getCouponId(), userId, orderInfo.getId());
		}
		
		//下单成功，记录用户购物商品数量，redis
		//hash类型   key(userId)  -  field(skuId)-value(skuNum)
		String orderSkuKey = RedisConstant.ORDER_SKU_MAP + orderParamVo.getUserId();
		BoundHashOperations<String, String, Integer> hashOperations = redisTemplate.boundHashOps(orderSkuKey);
		cartInfoList.forEach(cartInfo -> {
			if (Boolean.TRUE.equals(hashOperations.hasKey(cartInfo.getSkuId().toString()))) {
				Integer orderSkuNum = hashOperations.get(cartInfo.getSkuId().toString()) + cartInfo.getSkuNum();
				hashOperations.put(cartInfo.getSkuId().toString(), orderSkuNum);
			}
		});
		redisTemplate.expire(orderSkuKey, DateUtil.getCurrentExpireTimes(), TimeUnit.SECONDS);
		//订单id
		return orderInfo.getId();
	}
	
	//更新状态
	private void updateOrderStatus(Long id) {
		OrderInfo orderInfo = baseMapper.selectById(id);
		orderInfo.setOrderStatus(OrderStatus.WAITING_DELEVER.getCode());
		orderInfo.setProcessStatus(ProcessStatus.WAITING_DELEVER.getCode());
		baseMapper.updateById(orderInfo);
	}
	
	//计算总金额
	private BigDecimal computeTotalAmount(List<CartInfo> cartInfoList) {
		BigDecimal total = new BigDecimal(GlobalConstant.INTEGER_ZERO);
		for (CartInfo cartInfo : cartInfoList) {
			BigDecimal itemTotal = cartInfo.getCartPrice().multiply(new BigDecimal(cartInfo.getSkuNum()));
			total = total.add(itemTotal);
		}
		return total;
	}
	
	/**
	 * 计算购物项分摊的优惠减少金额
	 * 打折：按折扣分担
	 * 现金：按比例分摊
	 */
	private Map<String, BigDecimal> computeActivitySplitAmount(List<CartInfo> cartInfoParamList) {
		Map<String, BigDecimal> activitySplitAmountMap = new HashMap<>();
		
		//促销活动相关信息
		List<CartInfoVo> cartInfoVoList = activityFeignClient.findCartActivityList(cartInfoParamList);
		
		//活动总金额
		BigDecimal activityReduceAmount = new BigDecimal(GlobalConstant.INTEGER_ZERO);
		if (!CollectionUtils.isEmpty(cartInfoVoList)) {
			for (CartInfoVo cartInfoVo : cartInfoVoList) {
				ActivityRule activityRule = cartInfoVo.getActivityRule();
				List<CartInfo> cartInfoList = cartInfoVo.getCartInfoList();
				if (activityRule != null) {
					//优惠金额， 按比例分摊
					BigDecimal reduceAmount = activityRule.getReduceAmount();
					activityReduceAmount = activityReduceAmount.add(reduceAmount);
					if (cartInfoList.size() == GlobalConstant.INTEGER_ONE) {
						activitySplitAmountMap.put("activity:" + cartInfoList.get(GlobalConstant.INTEGER_ZERO).getSkuId(),
								reduceAmount);
					} else {
						//总金额
						BigDecimal originalTotalAmount = new BigDecimal(GlobalConstant.INTEGER_ZERO);
						for (CartInfo cartInfo : cartInfoList) {
							BigDecimal skuTotalAmount =
									cartInfo.getCartPrice().multiply(new BigDecimal(cartInfo.getSkuNum()));
							originalTotalAmount = originalTotalAmount.add(skuTotalAmount);
						}
						//记录除最后一项是所有分摊金额， 最后一项=总的 - skuPartReduceAmount
						BigDecimal skuPartReduceAmount = new BigDecimal(GlobalConstant.INTEGER_ZERO);
						if (activityRule.getActivityType().equals(ActivityType.FULL_REDUCTION.getCode())) {
							for (int i = GlobalConstant.INTEGER_ZERO, len = cartInfoList.size(); i < len; i++) {
								CartInfo cartInfo = cartInfoList.get(i);
								if (i < len - GlobalConstant.INTEGER_ONE) {
									BigDecimal skuTotalAmount =
											cartInfo.getCartPrice().multiply(new BigDecimal(cartInfo.getSkuNum()));
									//sku分摊金额
									BigDecimal skuReduceAmount =
											skuTotalAmount.divide(originalTotalAmount, GlobalConstant.INTEGER_TWO,
													RoundingMode.HALF_UP).multiply(reduceAmount);
									activitySplitAmountMap.put("activity:" + cartInfo.getSkuId(), skuReduceAmount);
									
									skuPartReduceAmount = skuPartReduceAmount.add(skuReduceAmount);
								} else {
									BigDecimal skuReduceAmount = reduceAmount.subtract(skuPartReduceAmount);
									activitySplitAmountMap.put("activity:" + cartInfo.getSkuId(), skuReduceAmount);
								}
							}
						} else {
							for (int i = GlobalConstant.INTEGER_ZERO, len = cartInfoList.size(); i < len; i++) {
								CartInfo cartInfo = cartInfoList.get(i);
								if (i < len - GlobalConstant.INTEGER_ONE) {
									BigDecimal skuTotalAmount =
											cartInfo.getCartPrice().multiply(new BigDecimal(cartInfo.getSkuNum()));
									//sku分摊金额
									BigDecimal skuDiscountTotalAmount =
											skuTotalAmount.multiply(activityRule.getBenefitDiscount().divide(new BigDecimal("10")));
									BigDecimal skuReduceAmount = skuTotalAmount.subtract(skuDiscountTotalAmount);
									activitySplitAmountMap.put("activity:" + cartInfo.getSkuId(), skuReduceAmount);
									
									skuPartReduceAmount = skuPartReduceAmount.add(skuReduceAmount);
								} else {
									BigDecimal skuReduceAmount = reduceAmount.subtract(skuPartReduceAmount);
									activitySplitAmountMap.put("activity:" + cartInfo.getSkuId(), skuReduceAmount);
								}
							}
						}
					}
				}
			}
		}
		activitySplitAmountMap.put("activity:total", activityReduceAmount);
		return activitySplitAmountMap;
	}
	
	//优惠卷优惠金额
	private Map<String, BigDecimal> computeCouponInfoSplitAmount(List<CartInfo> cartInfoList, Long couponId) {
		Map<String, BigDecimal> couponInfoSplitAmountMap = new HashMap<>();
		
		if (null == couponId) return couponInfoSplitAmountMap;
		CouponInfo couponInfo = activityFeignClient.findRangeSkuIdList(cartInfoList, couponId);
		
		if (null != couponInfo) {
			//sku对应的订单明细
			Map<Long, CartInfo> skuIdToCartInfoMap = new HashMap<>();
			for (CartInfo cartInfo : cartInfoList) {
				skuIdToCartInfoMap.put(cartInfo.getSkuId(), cartInfo);
			}
			//优惠券对应的skuId列表
			List<Long> skuIdList = couponInfo.getSkuIdList();
			if (CollectionUtils.isEmpty(skuIdList)) {
				return couponInfoSplitAmountMap;
			}
			//优惠券优化总金额
			BigDecimal reduceAmount = couponInfo.getAmount();
			if (skuIdList.size() == GlobalConstant.INTEGER_ONE) {
				//sku的优化金额
				couponInfoSplitAmountMap.put("coupon:" + skuIdToCartInfoMap.get(skuIdList.get(GlobalConstant.INTEGER_ZERO)).getSkuId(), reduceAmount);
			} else {
				//总金额
				BigDecimal originalTotalAmount = new BigDecimal(GlobalConstant.INTEGER_ZERO);
				for (Long skuId : skuIdList) {
					CartInfo cartInfo = skuIdToCartInfoMap.get(skuId);
					BigDecimal skuTotalAmount = cartInfo.getCartPrice().multiply(new BigDecimal(cartInfo.getSkuNum()));
					originalTotalAmount = originalTotalAmount.add(skuTotalAmount);
				}
				//记录除最后一项是所有分摊金额， 最后一项=总的 - skuPartReduceAmount
				BigDecimal skuPartReduceAmount = new BigDecimal(GlobalConstant.INTEGER_ZERO);
				if (couponInfo.getCouponType().equals(CouponType.CASH.getCode()) || couponInfo.getCouponType().equals(CouponType.FULL_REDUCTION.getCode())) {
					for (int i = GlobalConstant.INTEGER_ZERO, len = skuIdList.size(); i < len; i++) {
						CartInfo cartInfo = skuIdToCartInfoMap.get(skuIdList.get(i));
						if (i < len - GlobalConstant.INTEGER_ONE) {
							BigDecimal skuTotalAmount =
									cartInfo.getCartPrice().multiply(new BigDecimal(cartInfo.getSkuNum()));
							//sku分摊金额
							BigDecimal skuReduceAmount =
									skuTotalAmount.divide(originalTotalAmount, GlobalConstant.INTEGER_TWO,
											RoundingMode.HALF_UP).multiply(reduceAmount);
							couponInfoSplitAmountMap.put("coupon:" + cartInfo.getSkuId(), skuReduceAmount);
							
							skuPartReduceAmount = skuPartReduceAmount.add(skuReduceAmount);
						} else {
							BigDecimal skuReduceAmount = reduceAmount.subtract(skuPartReduceAmount);
							couponInfoSplitAmountMap.put("coupon:" + cartInfo.getSkuId(), skuReduceAmount);
						}
					}
				}
			}
			couponInfoSplitAmountMap.put("coupon:total", couponInfo.getAmount());
		}
		return couponInfoSplitAmountMap;
	}
}
